/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.thinkgem.jeesite.common.constant.Constant;
import com.thinkgem.jeesite.common.service.TreeService;
import com.thinkgem.jeesite.modules.sys.dao.AreaDao;
import com.thinkgem.jeesite.modules.sys.entity.Area;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 区域Service
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
@Transactional(readOnly = true)
public class AreaService extends TreeService<AreaDao, Area> {

	public List<Area> findAll(){
		return UserUtils.getAreaList();
	}

	@Transactional(readOnly = false)
	public void save(Area area) {
		super.save(area);
		UserUtils.removeCache(UserUtils.CACHE_AREA_LIST);
	}
	
	@Transactional(readOnly = false)
	public void delete(Area area) {
		super.delete(area);
		UserUtils.removeCache(UserUtils.CACHE_AREA_LIST);
	}
	
	// add by zad 2018-04-12 获取parentId下所有区域的最大编号
	public Integer getMaxSort(String parentId)
	{
		return dao.getMaxSort(parentId);
	}
	
	public Model fetchRegions(Model model)
	{
		Area area = UserUtils.getUser().getArea();
		model.addAttribute("area", area);
		Area param = new Area();
		Area p = new Area();
		p.setCode(area.getCode());
		param.setParent(p);
		// 操作员为县级时，只查询乡镇列表
		if(Constant.Area.AREA_TYPE_COUNTY.equals(area.getType()))
		{
			List<Area> regions = new ArrayList<Area>();
			regions.add(this.get(area.getParent().getId()));
			model.addAttribute("regions", regions);
			
			List<Area> countys = new ArrayList<Area>();
			countys.add(this.get(area.getId()));
			model.addAttribute("countys", countys);
			
			param.setType(Constant.Area.AREA_TYPE_TOWN);
			List<Area> towns = this.findList(param);
			model.addAttribute("towns", towns);
		}
		// 操作员为地市时，查询县及乡镇列表
		else if(Constant.Area.AREA_TYPE_REGION.equals(area.getType()))
		{
			List<Area> regions = new ArrayList<Area>();
			regions.add(this.get(area.getId()));
			model.addAttribute("regions", regions);
			
			param.setType(Constant.Area.AREA_TYPE_COUNTY);
			List<Area> countys = this.findList(param);
			model.addAttribute("countys", countys);
			
			p.setCode(countys.get(0).getCode());
			param.setType(Constant.Area.AREA_TYPE_TOWN);
			List<Area> towns = this.findList(param);
			model.addAttribute("towns", towns);
		}
		// 操作员为省时，查询县及乡镇列表
		else if(Constant.Area.AREA_TYPE_PROVINCE.equals(area.getType()))
		{
			param.setType(Constant.Area.AREA_TYPE_REGION);
			List<Area> regions = this.findList(param);
			model.addAttribute("regions", regions);
			
			p.setCode(regions.get(0).getCode());
			param.setType(Constant.Area.AREA_TYPE_COUNTY);
			List<Area> countys = this.findList(param);
			
			model.addAttribute("countys", countys);
			
			if(null != countys && countys.size() > 0)
			{
				p.setCode(countys.get(0).getCode());
				param.setType(Constant.Area.AREA_TYPE_TOWN);
				List<Area> towns = this.findList(param);
				model.addAttribute("towns", towns);
			}
		}
		return model;
	}
}
